//package com.lanmaoly.cunzheng.file;
//
//import org.apache.commons.codec.digest.DigestUtils;
//import org.apache.commons.io.IOUtils;
//
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.IOException;
//import java.nio.ByteBuffer;
//import java.nio.channels.FileChannel;
//import java.nio.charset.Charset;
//import java.nio.file.StandardOpenOption;
//
///**
// * 大文件
// * @author Administrator
// */
//public final class MyReader implements AutoCloseable {
//    private ByteBuffer byteBuffer = ByteBuffer.allocate(4096);
//    private final Charset charset;
//    private FileChannel fileChannel;
//    private volatile byte[] bufferByte = new byte[0];
//    private File file;
//    private volatile boolean positionIsEnd = false;
//
//    /**
//     * @param path The path of the text file.
//     * @throws IOException If the file does not exist, FileNotFoundException will be thrown.
//     */
//    public MyReader(String path) throws IOException {
//        this.file = new File(path);
//        fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.READ);
//        charset = Charset.forName("UTF-8");
//    }
//
//    /**
//     * @param file The text file Object.
//     * @throws IOException If the file does not exist, FileNotFoundException will be thrown.
//     */
//    public MyReader(File file) throws IOException {
//        this.file = file;
//        fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.READ);
//        charset = Charset.forName("UTF-8");
//    }
//
//    /**
//     * @param path    The path of the text file.
//     * @param charset The charset of the text file.
//     * @throws IOException If the file does not exist, FileNotFoundException will be thrown.
//     */
//    public MyReader(String path, Charset charset) throws IOException {
//        this.file = new File(path);
//        fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.READ);
//        this.charset = charset;
//    }
//
//    /**
//     * @param file    The text file Object.
//     * @param charset The charset of the text file.
//     * @throws IOException If the file does not exist, FileNotFoundException will be thrown.
//     */
//    public MyReader(File file, Charset charset) throws IOException {
//        this.file = file;
//        fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.READ);
//        this.charset = charset;
//    }
//
//
//    /**
//     * @return line
//     * @throws IOException io exception
//     */
//    public final String readLine() throws IOException {
//        checkPosition();
//        if (positionIsEnd && bufferByte.length == 0) {
//            return null;
//        }
//        byte[] bigBytes = getBigBytes();
//        int index = getIndex(bigBytes);
//        if (index == -1) {
//            if (positionIsEnd) {
//                return finalOperation();
//            } else {
//                bufferByte = bigBytes;
//                return readLine();
//            }
//        } else {
//            return getLine(bigBytes, index);
//        }
//    }
//
//    private void checkPosition() throws IOException {
//        if (fileChannel.position() == file.length()) {
//            positionIsEnd = true;
//        }
//    }
//
//    private String finalOperation() {
//        String tmp = new String(bufferByte, charset);
//        bufferByte = new byte[0];
//        return tmp;
//    }
//
//    private byte[] getBigBytes() throws IOException {
//        byteBuffer.clear();
//        fileChannel.read(byteBuffer);
//        byteBuffer.flip();
//        byte[] bytes = new byte[byteBuffer.limit()];
//        byteBuffer.get(bytes);
//        byte[] bigBytes = new byte[bufferByte.length + bytes.length];
//        System.arraycopy(bufferByte, 0, bigBytes, 0, bufferByte.length);
//        System.arraycopy(bytes, 0, bigBytes, bufferByte.length, bytes.length);
//        return bigBytes;
//    }
//
//    private String getLine(byte[] bigBytes, int index) {
//        String line = new String(bigBytes, 0, index, charset);
//        bufferByte = new byte[bigBytes.length - index - 1];
//        System.arraycopy(bigBytes, index + 1, bufferByte, 0, bufferByte.length);
//        return line;
//    }
//
//    private int getIndex(byte[] bigBytes) {
//        for (int i = 0; i < bigBytes.length; i++) {
//            if (bigBytes[i] == 10) {
//                return i;
//            }
//        }
//        return -1;
//    }
//
//    @Override
//    public void close() throws IOException {
//        fileChannel.close();
//    }
//
//    public static void main(String[] args) throws IOException {
//        File file = new File("D:\\lanmao_code\\用户关联.pub");
//        MyReader reader = new MyReader(file);
//        byte[] bigBytes = reader.getBigBytes();
//        System.out.println(DigestUtils.sha256Hex(bigBytes));
//
//        FileInputStream fis = new FileInputStream("D:\\lanmao_code\\用户关联.pub");
//        byte[] bytes = IOUtils.toByteArray(fis);
//        System.out.println(DigestUtils.sha256Hex(bytes));
//
//        reader.close();
//    }
//}
